package com.linghui.videoplus.ipai.service;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.linghui.videoplus.ipai.activity.MyVideoActivity;
import com.linghui.videoplus.ipai.api.json.JsonAPI;
import com.linghui.videoplus.ipai.bean.VideoBean;
import com.linghui.videoplus.ipai.utils.Constants;

public class UploadService extends Service {
	public Handler handler = null;

	@Override
	public void onCreate() {
		super.onCreate();
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1: // 进度条消息
					int status = (Integer) msg.obj;
					Intent intent = new Intent();
					intent.putExtra("MSG_WHAT", msg.what);
					intent.putExtra(Constants.EXTRA_UPLOAD_KEY, status);
					intent.setAction("android.intent.action.UPLOAD_RECEIVER");
					sendBroadcast(intent);
					break;
				case 2:
					msg.what=2;
					int progress = (Integer) msg.obj;
					Intent intent2 = new Intent();
					intent2.putExtra("MSG_WHAT", msg.what);
					intent2.putExtra("PROGRESS", progress);
					intent2.setAction("android.intent.action.UPLOAD_RECEIVER");
					sendBroadcast(intent2);
					break;
				}
			}
		};

	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		VideoBean videoBean = (VideoBean) intent.getSerializableExtra(Constants.EXTRA_UPLOAD_ENTRY);
		String uid = intent.getStringExtra(Constants.EXTRA_UPLOAD_UID_KEY);
		String author = intent.getStringExtra(Constants.EXTRA_UPLOAD_AUTHOR_KEY);
		String title = intent.getStringExtra(Constants.EXTRA_UPLOAD_TITLE_KEY);
		String desc = intent.getStringExtra(Constants.EXTRA_UPLOAD_DESC_KEY);
		Thread thread = new Thread(new UploadThread(UploadService.this,videoBean, handler,uid,author,title,desc));
		thread.start();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

}
